package com.focus.xyz.core.common.listeners;

import com.focus.xyz.core.utils.prop.ConfigPropInfo;
import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.NoSuchElementException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

/**
 * Created by chenmeng on 2016/5/20.
 */
public class ResultLogListener extends TestListenerAdapter {
    private transient static Logger logger = LoggerFactory.getLogger(ResultLogListener.class);

    @Override
    public void onTestFailure(ITestResult tr) {
        String filePath = takeScreenShot(tr);
        if (tr.getThrowable() != null) {
            StringBuffer sb = new StringBuffer();
            if (tr.getThrowable() instanceof NoSuchElementException) {
                sb.append("页面元素定位失败。");
            } else if (tr.getThrowable() instanceof AssertionError) {
                sb.append("断言错误。");
            }
//            sb.append("错误截图地址：" + filePath);
            sb.append("错误截图地址：" + "file://192.168.27.46/error/" + StringUtils.substringAfterLast(filePath,"/"));
            tr.setThrowable(new Exception(sb.toString(), tr.getThrowable()));
        }
        logger.info(tr.getInstanceName() + "." + tr.getMethod().getMethodName() + ":FAILURE");
        super.onTestFailure(tr);
    }

    @Override
    public void onTestSkipped(ITestResult tr) {
        String filePath = takeScreenShot(tr);
        if (tr.getThrowable() != null) {
            StringBuffer sb = new StringBuffer();
            if (tr.getThrowable() instanceof NoSuchElementException) {
                sb.append("页面元素定位失败。");
            }
//            sb.append("错误截图地址：" + filePath);
            sb.append("错误截图地址：" + "file://192.168.27.46/error/" + StringUtils.substringAfterLast(filePath,"/"));
            tr.setThrowable(new Exception(sb.toString(), tr.getThrowable()));
        }
        logger.info(tr.getInstanceName() + "." + tr.getMethod().getMethodName() + ":SKIPPED");
        super.onTestSkipped(tr);
    }

    @Override
    public void onTestSuccess(ITestResult tr) {
        logger.info(tr.getInstanceName() + "." + tr.getMethod().getMethodName() + ":SUCCESS");
        super.onTestSuccess(tr);
    }

    @Override
    public void onTestStart(ITestResult tr) {
        System.out.println("-----------------------------------------------------------------------------------------------------------------");
        super.onTestStart(tr);
    }

    @Override
    public void onFinish(ITestContext testContext) {
        System.out.println("-----------------------------------------------------------------------------------------------------------------");
        super.onFinish(testContext);
    }

    private String takeScreenShot(ITestResult tr) {
        TC b = (TC) tr.getInstance();
        String insName = tr.getInstanceName();
        if (StringUtils.isNotBlank(insName)) {
            insName = StringUtils.substringAfterLast(insName, ".");
        }
        String filePath = ConfigPropInfo.TEST_DATA_DIR + "error/" + insName + "." + tr.getMethod().getMethodName() + "." + System.currentTimeMillis() + ".png";
        b.takeScreenShot(filePath);
        return filePath;
    }
}
